﻿using System;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Package;

namespace Expect.Vs2008.Extensions.LanguageService
{
    [ComVisible(true)]
    [Guid("732D3DC6-9818-4402-B062-084F02951C20")]
    public class ExpectLanguageService : Microsoft.VisualStudio.Package.LanguageService
    {
        private LanguagePreferences _preferences;

        private IScanner _scanner;
        /*
        public const string ServiceGuid = "732D3DC6-9818-4402-B062-084F02951C20";
        private static Guid _languageId = new Guid(ServiceGuid);
        public static Guid LanguageServiceID
        {
            get
            {
                return _languageId;
            }
        }*/

        public override string GetFormatFilterList()
        {
            return "Expect File (*.expect)|*.expect";
        }

        public override LanguagePreferences GetLanguagePreferences()
        {
            if (this._preferences == null)
            {
                this._preferences = new LanguagePreferences(this.Site, typeof(ExpectLanguageService).GUID, this.Name);                
                this._preferences.Init();
            }

            return this._preferences;
        }

        public override void OnIdle(bool periodic)
        {
            // from IronPythonLanguage sample
            // this appears to be necessary to get a parse request with ParseReason = Check?
            Source src = (Source)GetSource(this.LastActiveTextView);
            if (src != null && src.LastParseTime >= Int32.MaxValue >> 12)
            {
                src.LastParseTime = 0;
            }
            base.OnIdle(periodic);
        }

        public override IScanner GetScanner(Microsoft.VisualStudio.TextManager.Interop.IVsTextLines buffer)
        {
            if (_scanner == null)
            {
                _scanner = new ExpectScanner();
            }

            return _scanner;
        }

        public override string Name
        {
            get
            {
                return "Expect Language";
            }
        }

        public override AuthoringScope ParseSource(ParseRequest req)
        {
            Source source = this.GetSource(req.FileName);
            
            bool yyparseResult = false;
            
            var res = new ExpectAuthoringScope();
            
            /*Parser parser = new Parser();
            parser.SetParseRequest(req);
            parser.Parse();*/

            return res;
        }
    }
}
